-- Single-Row or Scalar Subqueries in a WHERE clause
---- What is the name of the most expensive product 
SELECT name AS "Product Name"
    ,TO_CHAR(retailprice,'$9,999.99') AS "Most Expensive"
FROM Products
WHERE retailprice = (SELECT MAX(retailprice) FROM products);

---- Scalar Sub Query in a SELECT clause
-- Customer lastnames from Oct 27th orders
SELECT o.Id
    ,o.OrderDate
    ,shipdate
    ,(SELECT lastname FROM Customers WHERE Customers.Id = o.CustomerId) AS Customer
FROM Orders o 
WHERE o.shipdate = '2020-10-27';


-- Multiple Row Sub queries (Sets)
---- IN Operator within a WHERE clause
-- What other products were ordered with an Ash Baseball bat, product #2
SELECT DISTINCT name AS "Products Ordered with Ash Baseball Bat"
FROM Orders o 
    JOIN Order_Details od
        ON o.Id = od.OrderId 
    JOIN Products p 
        ON od.ProductId = p.Id 
WHERE o.Id IN (SELECT OrderId FROM Order_Details WHERE ProductId = 2)
    AND p.Id <> 2;

-- Exists Operator
-- Find all customers who ordered baseball equipment with Category #2
SELECT Id
    ,FirstName
    ,Lastname
FROM Customers
WHERE EXISTS 
    (SELECT * FROM Orders INNER JOIN Order_Details ON Orders.Id = Order_Details.OrderId
     INNER JOIN Products ON Order_Details.ProductId = Products.Id 
     WHERE Products.CategoryId = 2 AND Orders.CustomerId = Customers.Id);


---- Views -----
-- Create a View ---
CREATE OR REPLACE VIEW vwInventory
AS
SELECT Id, name, qtyonhand, retailprice
FROM Products 
WITH READ ONLY;

SELECT * FROM vwInventory
WHERE RETAILPRICE >= 40;

-- Ensure READ ONLY works
UPDATE vwInventory SET qtyonhand = 0
WHERE Id = 2;

-- a View is treated like a table 
SELECT Id, TO_CHAR(retailprice, '$9,999.99') AS "Price"
FROM vwInventory;

-- View based on 2 tables
CREATE OR REPLACE VIEW vwCustomerOrders
AS 
SELECT FirstName, LastName, Orders.Id, OrderDate
FROM Customers JOIN Orders ON customers.Id = Orders.CustomerId
WITH READ ONLY

SELECT * FROM vwCustomerOrders

-- Remove vwInventory View
DROP VIEW vwInventory; 


-- Oracle PL/SQL
-- Example #1 – The classic Hello World in a PL/SQL Anonymous Block
DECLARE
    msg VARCHAR2(100) := 'Hello World!';
BEGIN
    dbms_output.put_line('My Message is: ' || msg);
    EXCEPTION 
        WHEN OTHERS THEN 
        dbms_output.put_line(SQLERRM);
END;

SELECT * FROM user_errors;

-- Using a Stored Procedure
-- Create Stored Procedure so we can run it numerous times
CREATE OR REPLACE PROCEDURE hello_world IS 
    msg VARCHAR2(100) := 'Hello World';
BEGIN
    dbms_output.put_line(msg);
    EXCEPTION 
        WHEN OTHERS THEN 
        dbms_output.put_line(SQLERRM);
END hello_world;

-- Run the procedure multiple time in an Anonymous block 
-- note no declaration or error sections this time 
BEGIN 
    hello_world;
    hello_world;
    hello_world;
END;

DROP PROCEDURE hello_world;
DROP PROCEDURE cursor_example;

-- Cursors - Embedding a Query in a Procedure
CREATE OR REPLACE PROCEDURE cursor_example IS 
-- declare variables here using type or from existing column meta data 
    o_id    VARCHAR2(4);
    o_date  orders.orderdate%type;
    -- define cursor for SELECT
    CURSOR c_customer1001orders IS 
        SELECT LPAD(Id, 4, ' '), OrderDate 
        FROM Orders WHERE CustomerId = 1001;
-- Execution begins here
BEGIN
    -- Heading lines, Chr(10) is an new line code 
    dbms_output.put_line('Customer''s 1001 Orders' || chr(10));
    dbms_output.put_line('Order#' || '        ' || '  Date  ' || chr(10));

    -- Execute SELECT CURSOR 
    OPEN c_customer1001orders;
    -- retrieve each row from the resultset in a loop
    LOOP 
        FETCH c_customer1001orders INTO o_id, o_date;
        -- End of resultset reached?
        EXIT WHEN c_customer1001orders%notfound;
        -- print out data 
        dbms_output.put_line(o_id || '      ' || o_date);
    END LOOP;
    -- close the CURSOR 
    CLOSE c_customer1001orders;
END;

-- Run the procedure cursor example in an Anonymous block 
-- note no declaration or error sections this time 
BEGIN 
    cursor_example;
END;

-- Practice for the Lab 9 Quiz
-- Q1)
CREATE OR REPLACE VIEW practice_view
AS  
SELECT o.Id AS "OrderID"
    ,o.OrderDate 
    ,e.LastName
    ,e.FirstName
FROM Orders o JOIN Employees e ON e.Id = o.EMPLOYEEID
WHERE o.EMPLOYEEID IN (SELECT Id FROM Employees WHERE LastName LIKE 'D%');

-- Run the View
SELECT * FROM practice_view
ORDER BY 1;

-- Q2)
CREATE OR REPLACE PROCEDURE p_practice IS 
    -- declare variables here using type or from existing column meta data 
    o_id    VARCHAR2(6);
    o_date  orders.orderdate%type;
    e_last  Employees%LastName;
    e_first Employees%FirstName;

    -- define Cursor from VIEW 
    CURSOR c_orderssoldbyemployees IS 
        SELECT LPAD(Id, 6, ' '), OrderDate, LastName, FirstName
        FROM practice_view;

-- Execution begins here
BEGIN
    -- Heading lines, Chr(10) is an new line code 
    dbms_output.put_line('Osam Ali''s Practice Lab - Orders By Employees' || chr(10));
    dbms_output.put_line('  #  ' || '     ' || 'Order Date' || '    Employee Name ' || chr(10));
    
    OPEN c_orderssoldbyemployees;
    -- retrieve each row from the resultset in a loop
    LOOP 
        FETCH c_orderssoldbyemployees INTO o_id, o_date;
        -- End of resultset reached?
        EXIT WHEN c_orderssoldbyemployees%notfound;
        -- print out data 
        dbms_output.put_line(o_id || '      ' || o_date);
    END LOOP;
    -- close the CURSOR 
    CLOSE c_orderssoldbyemployees;

END;





